package com.innovation.config;

import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class MessageConfig {
	/**
	 * 错误信息缓存
	 */
	private static Map<Integer, String> errorMsgMap = new HashMap<Integer, String>();
	
	public static void init() throws Exception{
		//错误提示信息加载
		initErrorMessage();
	}
	
	/**
	 * 加载错误信息提示
	 * @throws Exception
	 */
	private static void initErrorMessage() throws Exception{
		Properties properties = new Properties();
		InputStreamReader isr = null;
		try{
			isr = new InputStreamReader(MessageConfig.class.getClassLoader().getResourceAsStream("error_message.properties"),"UTF-8");
			properties.load(isr);
			for(Object key : properties.keySet()){
				int keyCode = Integer.valueOf(key.toString()); 
				String msg = properties.getProperty(key.toString());
				errorMsgMap.put(keyCode, msg);
			}
		}finally{
			if(isr != null){
				isr.close();
			}
		}
	}
	
	/**
	 * 根据错误码获取错误信息
	 * @param code
	 * @return
	 */
	public static String getErrorMessage(int code){
		return errorMsgMap.get(code);
	}
}
